import { Style, Fill, Stroke, Text, Icon } from 'ol/style';
import { colorRgb } from '../util'
import { mapManager } from '../MapManager';
import Circle from 'ol/style/Circle';
import { Point, MultiPoint } from 'ol/geom'
const PARTITIONS_Z = 760;
/**
 * 分区样式管理
 */
class PartitionStyle {

    static defaultColor = "#ffffff";

     /**
     * 分区规划中polygon 样式
     */
      static drawPartitionStyle () {
        return [new Style({
            stroke: new Stroke({
                color: PartitionStyle.defaultColor,
                lineDash: [10, 10],
                width: 2
            }),
            fill: new Fill({
                color: "rgba(255,255,255, 0.45)"
            }),
            zIndex: PARTITIONS_Z
        }),
        borderPointStyle1,borderPointStyle2
    ]
    }

    /**
     * 获取样式
     * @param {import("../../../util/typedef").PartitionInfo} partition  分区信息
     */
    static getStyle(partition) {
        return function (feature) {
          
            var color = partition.viewColor == "" || partition.viewColor == null ? PartitionStyle.defaultColor : partition.viewColor;
            var fontSize = 14;
            var fontPadding =   [5, 5, 5, 5];
            var storkeWidth = 1;
            var lineDash = null;
            var opacity = partition.opacity;

            var lineDashed = feature.get('lineDashed'); //获取属性，是否为虚线
            if (lineDashed) {
                lineDash = [10, 10];
            }

            var zoom = mapManager.getMap().getView().getZoom();
            if (zoom > 16) {
                opacity = 0;
            }


            if(zoom <15){
                fontSize = 12;
                fontPadding = [2,2,2,2]
            }

            
            if(zoom <14){
                fontSize = 10;
                fontPadding = [1,1,1,1]
            }

            if(zoom <13){
                fontSize = 0;
                fontPadding = [0,0,0,0]
            }

            if(zoom <11.8){
                storkeWidth = 0;
                opacity = 0;
            }
 

            var style = [new Style({
                stroke: new Stroke({
                    color: color,
                    width: storkeWidth,
                    lineDash: lineDash
                }),
                fill: new Fill({
                    color: "rgba(" + colorRgb(color) + ", " + opacity + ")"
                }),
                text: new Text({
                    font: fontSize+'px sans-serif',
                    text: partition.name,
                    padding: fontPadding,
                    backgroundFill: new Fill({
                        color: "rgba(255,255,255,0.6)"
                    })
                }),
                zIndex: 1
            })];

            if(lineDashed){
                style.push(borderPointStyle1);
                style.push(borderPointStyle2);
            }

            return style;
        }
    }
}


const borderPointStyle1 = new Style({
    image: new Circle({
        radius: 5,
        fill: new Fill({
            color: '#fff'
        })
    }),
    geometry: function (feature) {
        var type = feature.getGeometry().getType();
        var coordinates = feature.getGeometry().getCoordinates()
        if(type == 'Point'){
            return new MultiPoint([coordinates]);
        }else{
          return new MultiPoint(coordinates[0]);
        }
    }
});


const borderPointStyle2 =   new Style({
    image: new Circle({
        radius: 5,
        fill: new Fill({
            color: 'yellow'
        })
    }),
    geometry: function (feature) {
        var type = feature.getGeometry().getType();
        var coordinates = feature.getGeometry().getCoordinates()
        if(type == 'Point'){
            return new Point(coordinates);
        }else{
          return new Point(coordinates[0][0]);
        }
    }
});
export default PartitionStyle;